//
//  Init.c
//  ARDUINO_PROPELLER
//
//  Created by FRANCIS PAPINEAU on 12-02-06.
//  Copyright 2012 carleton university. All rights reserved.
//

//! ------------------------------------------------------------------------------

//! These functions, are the initializers of the system, they act as the boot up
//! also there are some checking done to allow for redundency, and to make sure
//! our system is working correctly. All status is sent to the CPU to logging
//! and analysis.

//! ------------------------------------------------------------------------------

bool initComms(void){
    
    checkVar = false;
    //! setup comms and Terminals
    
    Serial.begin(SERIAL_BAUD);
    if(Serial.available()) checkVar = true;
    
    Serial2.begin(SERIAL_BAUD);
    if(Serial2.available()) checkVar = checkVar & true;
    
    if(checkVar){
        return true;
    }else{
        sendError(checkVar, CommsUpAddress);
        return false;
    }  
}

//! ------------------------------------------------------------------------------

bool initLEDPins(void){
    
    checkVar = false;
    //! setup LEDs
    
    pinMode(LEDPWR, OUTPUT); digitalWrite(LEDPWR, LOW);
    pinMode(LEDStandby, OUTPUT); digitalWrite(LEDStandby, LOW);
    pinMode(LEDError, OUTPUT); digitalWrite(LEDError, LOW);
    pinMode(LEDOFF, OUTPUT); digitalWrite(LEDOFF, LOW);
    pinMode(LEDDEBUG, OUTPUT); digitalWrite(LEDDEBUG, LOW);
    
    if(checkVar){
        return true;
    }else{
        sendError(checkVar, LEDPinsUpAddress);
        return false;
    }  
}

//! ------------------------------------------------------------------------------

bool checkAnalogSensors(void){
    
    checkVar = false;
    int tempReading = 0;
    //! check Analog devices
    
    for(int i = 0; i < 8; i++){
        tempReading = analogRead(i);
        if(tempReading != 0){
            checkVar = true;
        }
    }
    
    if(checkVar){
        return true;
    }else{
        sendError(checkVar, AnalogCheckedAddress);
        return false;
    }  
}

//! ------------------------------------------------------------------------------

bool checkLEDPins(void){
    
    checkVar = false;
    //! check LED pins
    
    togglePWRLED();delay(500);
    
    toggleStandbyLED();delay(500);

    toggleERRORLED();delay(500);

    toggleRUNLED();delay(500);

    toggleDEBUGLED();delay(500);

    toggleRunLED();delay(500);

    toggleOff();delay(500);

    toggleAllLED();delay(500);

    toggleOff();delay(500);

    
    if(checkVar){
        return true;
    }else{
        sendError(checkVar, LEDCheckedAddress);
        return false;
    }  
}

//! ------------------------------------------------------------------------------
//! ------------------------------------------------------------------------------
//! ------------------------------------------------------------------------------
//! ------------------------------------------------------------------------------

void togglePWRLED(void){
    
    //! this function toggles the Startup LED
    if (!setDigitalSelectBits(1)){
        sendError(false, RunTimeAddress);    
    }
}

//! ------------------------------------------------------------------------------

void toggleStandbyLED(void){
    
    //! this function toggles the Standby LED
    if (!setDigitalSelectBits(2)){
        sendError(false, RunTimeAddress);
    }
}

//! ------------------------------------------------------------------------------

void toggleRUNLED(void){
    
    //! this function toggles the RUN LED
    if (!setDigitalSelectBits(6)){
        sendError(false, RunTimeAddress); 
    }
}

//! ------------------------------------------------------------------------------

void toggleERRORLED(void){
    
    //! this function toggles the ERROR LED
    if (!setDigitalSelectBits(3)){
        sendError(false, RunTimeAddress);  
    }
}

//! ------------------------------------------------------------------------------

void toggleDEBUGLED(void){
    
    //! this function toggles the ERROR LED
    if (!setDigitalSelectBits(5)){
        sendError(false, RunTimeAddresss);
    }
}

//! ------------------------------------------------------------------------------

void toggleAllLED(void){
    
    //! this function toggles the ERROR LED
    if (!setDigitalSelectBits(7)){
        sendError(false, RunTimeAddresss);
    }
}

//! ------------------------------------------------------------------------------

void toggleRunLED(void){
    
    //! this function toggles the ERROR LED
    if (!setDigitalSelectBits(6)){
        sendError(false, RunTimeAddresss);
    }
}

//! ------------------------------------------------------------------------------

void toggleOff(void){
    
    //! this function toggles the ERROR LED
    if (!setDigitalSelectBits(4)){
        sendError(false, RunTimeAddresss);
    }
}

//! ------------------------------------------------------------------------------

void changeState(void){
    
    //! this function advances the State of the MCU
    State = (State + 1)%6;
    sendState();
}

//! ------------------------------------------------------------------------------

void changeStateTo(int value){
    
    //! this function advances the State of the MCU
    State = value;
    sendState();
}

